<?php
class Place_ApprovePlaceController extends Zend_Controller_Action{
        
    public function init(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        $userSession = new Zend_Session_Namespace('user_session');
        $userSession->setExpirationSeconds($constants->SESSION_TIMEOUT);
        $user = $userSession->user;
        $userId = $user->sale_user_id;
        try{
            if(isset($userId)){
                
                $userRoleModel = new Place_Model_SaleUserRole();
                $userRole = $userRoleModel->getRoleOfUser($userId);
                
                $saleUserRoleFeature = new Place_Model_SaleUserRoleFeature();
                $featureArr = $saleUserRoleFeature->getFeatureOfUserRole($userRole);
                $featureIdArr = array();
                foreach ($featureArr as $key => $value) {
                    array_push($featureIdArr,$value->sale_feature_id);
                }
                if(!in_array($constants->FEATURE_APPROVE_PLACE, $featureIdArr)){
                    $this->_redirect('/place/index/index');
                }
                
                $this->view->title = "Approve Place";    
                $this->view->userName = $user->full_name;
                
            }else{
                $this->_redirect('/place/index/index');
            }   
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }

    public function indexAction(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        try{
            
            $userSession = new Zend_Session_Namespace('user_session');
            $user = $userSession->user;
            $userId = $user->sale_user_id;
            
            $userRoleModel = new Place_Model_SaleUserRole();
            $userRole = $userRoleModel->getRoleOfUser($userId);
            
            $saleUserRoleFeature = new Place_Model_SaleUserRoleFeature();
            $featureArr = $saleUserRoleFeature->getFeatureOfUserRole($userRole);
            $featureIdArr = array();
            foreach ($featureArr as $key => $value) {
                array_push($featureIdArr,$value->sale_feature_id);
            }
            if(!in_array($constants->FEATURE_APPROVE_PLACE, $featureIdArr)){
                $this->_redirect('/place/index/index');
            }
            
            $this->view->userFeature = $featureArr;
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
        
        
    }
    
    public function approveAction(){
        $this -> _helper -> viewRenderer -> setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        try{
            
            $normalUtils = new My_Utils_NormalUtils();
            $DB = $normalUtils->getNormalDb(1);
            
            $APPROVED = $constants -> APPROVED;
            if (isset($_GET['place_list'])) {
                $placeList = $_GET['place_list'];
                $place_id_str_list = explode(",", $placeList);
                $places_id_list = implode(",", array_map("intval", $place_id_str_list));
                $deleteQuery = "UPDATE merchant_place SET status = $APPROVED " . " WHERE place_id IN ($places_id_list) ";
                $DB -> query($deleteQuery);
                echo $registry -> constants -> MERCHANT_APPROVE_SUCCESS;
            } else {
                echo $registry -> constants -> ERROR_STRING;
            }
            
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }


    public function approveAndNextAction(){
        $this -> _helper -> viewRenderer -> setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        $DB -> beginTransaction();
        try{
            if(!isset($_GET['place_id'])){
                throw new Exception($constants->ERROR_STRING);
            }
            
            $placeId = intval($_GET['place_id']);
            if($placeId == '' || $placeId == 0){
                throw new Exception($constants->ERROR_STRING);
            }
            $approveQuery = "UPDATE merchant_place SET `status` = $constants->APPROVED".
                            " WHERE place_id = $placeId";
            $DB->query($approveQuery);
            $merchantPlaceModel = new Place_Model_MerchantPlace();
            $nextPlaceId = $merchantPlaceModel->getNextPlaceId($placeId);
            
            echo $nextPlaceId;
            $DB -> commit();
        }catch(Exception $e){
            $DB -> rollback();
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }
    
    public function rejectAndNextAction(){
        $this -> _helper -> viewRenderer -> setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        $normalUtils = new My_Utils_NormalUtils();
        $DB = $normalUtils->getNormalDb(1);
        $DB -> beginTransaction();
        try{
            
            if(!isset($_GET['place_id'])){
                throw new Exception($constants->ERROR_STRING);
            }
            
            $placeId = intval($_GET['place_id']);
            if($placeId == '' || $placeId == 0){
                throw new Exception($constants->ERROR_STRING);
            }
            $approveQuery = "UPDATE merchant_place SET `status` = $constants->REJECT_APPROVE".
                            " WHERE place_id = $placeId";
            $DB->query($approveQuery);
            $merchantPlaceModel = new Place_Model_MerchantPlace();
            $nextPlaceId = $merchantPlaceModel->getNextPlaceId($placeId);
            
            echo $nextPlaceId;
            $DB -> commit();
            
        }catch(Exception $e){
            $DB -> rollback();
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }
    
    public function rejectAction(){
        $this -> _helper -> viewRenderer -> setNoRender(true);
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        try{
            
            $normalUtils = new My_Utils_NormalUtils();
            $DB = $normalUtils->getNormalDb(1);
            
            $REJECT_APPROVE = $constants -> REJECT_APPROVE;
            if (isset($_GET['place_list'])) {
                $placeList = $_GET['place_list'];
                $place_id_str_list = explode(",", $placeList);
                $places_id_list = implode(",", array_map("intval", $place_id_str_list));
                $deleteQuery = "UPDATE merchant_place SET status = $REJECT_APPROVE " . " WHERE place_id IN ($places_id_list) ";
                $DB -> query($deleteQuery);
                echo $registry -> constants -> MERCHANT_REJECT_SUCCESS;
            } else {
                echo $registry -> constants -> ERROR_STRING;
            }
            
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
        
        
    }

    public function placeDetailAction(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        try{
            if(!isset($_GET['place_id'])){
                throw new Exception($constants->ERROR_STRING);
            }
            
            $placeId = $_GET['place_id'];
            $merchantPlaceModel = new Place_Model_MerchantPlace();
            $place = $merchantPlaceModel->getPlaceDetail($placeId);
            
            $saleId = $place->sale_user_id;
            
            $saleUserModel = new Place_Model_SaleUser();
            $saleUser = $saleUserModel->getSaleUserById($saleId);
            
            $userSession = new Zend_Session_Namespace('user_session');
            $user = $userSession->user;
            $userId = $user->sale_user_id;
            
            $userRoleModel = new Place_Model_SaleUserRole();
            $userRole = $userRoleModel->getRoleOfUser($userId);
            
            $saleUserRoleFeature = new Place_Model_SaleUserRoleFeature();
            $featureArr = $saleUserRoleFeature->getFeatureOfUserRole($userRole);
            $featureIdArr = array();
            foreach ($featureArr as $key => $value) {
                array_push($featureIdArr,$value->sale_feature_id);
            }
            if(!in_array($constants->FEATURE_APPROVE_PLACE, $featureIdArr)){
                $this->_redirect('/place/index/index');
            }
            
            $this->view->userFeature = $featureArr;
            
            $this->view->place = $place;
            $this->view->placeId = $placeId;
            $this->view->saleUser = $saleUser;
            
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }

    public function getRequestPlaceAction(){
        $registry   = Zend_Registry::getInstance();
        $constants = $registry->constants;
        try{
            
            $userSession = new Zend_Session_Namespace('user_session');
            $user = $userSession->user;
            $userId = $user->sale_user_id;
            
            $constant = $registry->constants;
            $PAGING_NUMBER = $constant->PAGING_NUMBER;
            
            $page = 1;
            if(isset($_GET['page'])){
                $page = $_GET['page'];
            }
            
            $startNumber = ($page - 1)*$PAGING_NUMBER;
            
            $saleUserTeam = new Place_Model_SaleUserTeam();
            $teamIdList = $saleUserTeam->getTeamOfUser($userId);
            
            $userModel = new Place_Model_SaleUser();
            $allUser = $userModel->getAllSaleUser();
            
            $allUserArr = array();
            foreach ($allUser as $key => $value) {
                $allUserArr[$value->sale_user_id] = $value->login_name."-".$value->full_name;
            }
            
            $placeArray = array();
            $placeNumber = 0;
            $merchantPlaceModel = new Place_Model_MerchantPlace();
            
            foreach ($teamIdList as $key => $value) {
                $placeOfTeam = $merchantPlaceModel
                                ->getPlaceOfTeamByStatus($value, $constants->APPROVE_REQUEST, $page);
                $placeArray = array_merge($placeArray, $placeOfTeam);
                $placeNumber = $placeNumber + $merchantPlaceModel
                                                ->getPlaceNumberOfTeam($value, $constants->APPROVE_REQUEST);
            }
            
            $result = array();
            foreach ($placeArray as $key => $value) {
                $item = $value;
                $item->sale_user = $allUserArr[$value->sale_user_id];
                array_push($result, $item);
            }
            
            $pageNumber = ceil($placeNumber/ ($registry->constants->PAGING_NUMBER));
            $this->view->result = $result;
            $this->view->page = $page;
            $this->view->pageNumber = $pageNumber;
            
        }catch(Exception $e){
            $writer = new Zend_Log_Writer_Stream(BASE_PATH.$constants->LOG_FOLDER.'production_'.date('Y_m_d').$constants->LOG_FILE_EXTENSION);
            $logger = new Zend_Log($writer);
            $controller = $this->getRequest()->getControllerName();
            $action = $this->getRequest()->getActionName();
            $logger -> info($controller."|".$action."|".$e -> getMessage());
            echo $constants->ERROR_STRING;
        }
    }

    
}
